package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.Manager;
import com.woniu.entity.ManagerRole;
import com.woniu.entity.User;
import com.woniu.mapper.ManagerMapper;
import com.woniu.mapper.ManagerRoleMapper;
import com.woniu.mapper.UserMapper;
import com.woniu.service.ManagerService;

import com.woniu.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


/**
 * <p>
 * 内部员工信息 服务实现类
 * </p>
 *
 * @author D
 * @since 2023-03-27 08:24:57
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class ManagerServiceImpl extends ServiceImpl<ManagerMapper, Manager> implements ManagerService {
    private final Manager manager = new Manager();
    public final ManagerRole managerRole = new ManagerRole();
    @Autowired
    UserMapper mapper;
    @Autowired
    UserService userService;
    @Autowired
    ManagerMapper managerMapper;

    @Autowired
    ManagerRoleMapper managerRoleMapper;

    /**
     * 分页查询用户列表
     *
     * @param c       当前页数
     * @param s       每页显示数量
     * @param keyword 关键字
     * @return 用户分页对象
     */
    @Override
    public Page<User> userPageList(Long c, Integer s, String keyword) {
        // 如果关键字为空，则查询所有用户
        if (keyword == null || "".equals(keyword)) {
            return mapper.selectPage(new Page<User>(c, s), new QueryWrapper<User>()
                    .orderByDesc("total_score"));
        }
        // 如果关键字不为空，则根据关键字查询用户
        return mapper.selectPage(new Page<User>(c, s), new QueryWrapper<User>()
                .like("username", keyword)
                .orderByDesc("total_score"));
    }

    /**
     * 初始化 Manager 对象
     *
     * @param user User 对象
     * @return 初始化后的 Manager 对象
     */
    public Manager initManager(User user) {
        manager.setId(null);
        manager.setLocked("0");
        manager.setUserPassword(user.getPassword());
        manager.setInsertTime(new Date());
        manager.setUserCode(user.getUserCode().toString());
        manager.setUserName(user.getUsername());
        manager.setCellphone(user.getPhoneNumber());
        return manager;
    }

    /**
     * 初始化 ManagerRole 对象
     *
     * @param id 管理员 ID
     * @return 初始化后的 ManagerRole 对象
     */
    public ManagerRole initManagerRole(Long id) {
        managerRole.setId(null);
        managerRole.setManagerId(id);
        managerRole.setRoleId(-1L);
        managerRole.setInsertTime(new Date());
        return managerRole;
    }
    /**
     * 添加管理员，同时向Manager表、ManagerRole表、User表添加记录
     * @param user 需要添加的用户信息
     * @return 添加记录条数
     */
    @Override
    public int addManager(User user) {
        //User表添加
        userService.save(user);
        log.info("code:" + user.getUserCode());

        Manager manager2 = initManager(user);
        //Manager表添加
        managerMapper.insert(manager2);
        //查询当前用户
        Manager manager1 = managerMapper.listOneByCode(user.getUserCode());
        //到查询失败时
        if (manager1 == null) return 0;
        //成功时
        Long id = manager1.getId();
        //ManagerRole表添加
        ManagerRole managerRole1 = initManagerRole(id);
        managerRoleMapper.insert(managerRole1);
        //user表添加
        return 1;
    }

    @Override
    public Manager initUpdate(Long id) {
        Manager list = managerMapper.list(id);
        log.info("id是谁:" + id);
        if (list == null) {
            User user = mapper.selectById(id);
            Manager manager2 = initManager(user);
            //Manager表添加
            managerMapper.insert(manager2);
            //查询当前用户
            Manager manager1 = managerMapper.listOneByCode(user.getUserCode());
            //到查询失败时
            if (manager1 == null) return null;
            //成功时
            Long manager1Id = manager1.getId();
            //ManagerRole表添加
            ManagerRole managerRole1 = initManagerRole(manager1Id);
            managerRoleMapper.insert(managerRole1);
            System.out.println("list:" + managerMapper.list(id).getManagerRoleList().size());
            return managerMapper.list(id);
        }
        System.out.println("list:" + list.getManagerRoleList().size());
        return list;
    }

    @Override
    public Manager selectById(Long id) {
        return managerMapper.listOneById(id);
    }

    @Override
    public int updateManager(Manager manager) {
        return managerMapper.update(manager, new QueryWrapper<Manager>().eq("id", manager.getId()));
    }

    @Override
    public boolean delAll(Long userCode) {
        //删除user表
        Manager manager = managerMapper.listOneByCode(userCode);
        Long id = manager.getId();
        int c3 = managerRoleMapper.delete(new QueryWrapper<ManagerRole>().eq("manager_id", id));
        int c1 = mapper.delete(new QueryWrapper<User>().eq("user_code", userCode));
        int c2 = managerMapper.delete(new QueryWrapper<Manager>().eq("user_code", userCode));

        if (c1 != 0 && c2 != 0 && c3 != 0) {
            return true;
        }
        return false;
    }

    @Override
    public Manager getByCellphone(String phoneNumber) {
        return managerMapper.selectOne(new QueryWrapper<Manager>().eq("cellphone", phoneNumber));
    }

    @Override
    public Manager selectEndowRole(Long userCode) {
        return managerMapper.list(userCode);
    }

}
